package org.wangchaocheng.cloudgallery.user.gatewayimpl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.wangchaocheng.cloudgallery.dto.user.UserRoleDTO;
import org.wangchaocheng.cloudgallery.user.converter.UserRoleConverter;
import org.wangchaocheng.cloudgallery.user.dataObject.UserRoleDO;
import org.wangchaocheng.cloudgallery.user.gateway.UserRoleGateway;
import org.wangchaocheng.cloudgallery.user.mapper.UserRoleMapper;

/**
 * @author wangchaocheng
 * @date 2025-03-01 10:30:19
*/
@Repository
public class UserRoleGatewayImpl extends ServiceImpl<UserRoleMapper, UserRoleDO> implements UserRoleGateway {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchSave(List<UserRoleDTO> userRoleDTOList) {
        // 转换模型
        List<UserRoleDO> userRoleDOList = UserRoleConverter.INSTANCE.dtoList2DoList(userRoleDTOList);
        this.saveOrUpdateBatch(userRoleDOList);
    }

    @Override
    public void removeByUserId(Long userId) {
        LambdaQueryWrapper<UserRoleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRoleDO::getUserId, userId);
        this.baseMapper.delete(queryWrapper);
    }

    @Override
    public UserRoleDTO findByUserIdAndRoleId(Long userId, Long roleId) {
        if (Objects.isNull(userId) || Objects.isNull(roleId)) {
            return null;
        }
        LambdaQueryWrapper<UserRoleDO> queryWrapper = new LambdaQueryWrapper<>();
        // 查询条件
        queryWrapper.eq(UserRoleDO::getUserId, userId).eq(UserRoleDO::getRoleId, roleId);
        // 查询
        UserRoleDO userRoleDO = this.baseMapper.selectOne(queryWrapper);
        return UserRoleConverter.INSTANCE.do2Dto(userRoleDO);
    }

    @Override
    public List<UserRoleDTO> listByUserId(Long userId) {
        LambdaQueryWrapper<UserRoleDO> queryWrapper = new LambdaQueryWrapper<>();
        // 查询条件
        queryWrapper.eq(UserRoleDO::getUserId, userId);
        List<UserRoleDO> userRoleList = this.baseMapper.selectList(queryWrapper);
        // 转换
        return UserRoleConverter.INSTANCE.doList2DtoList(userRoleList);
    }

}




